const mysql = require("mysql");
const sql = require("../db/sql.js"); //
const $dbConfig = require("../db/dbConfig.js"); //注意改成自己项目中mysql配置文件的路径
const mysqlDB = mysql.createPool($dbConfig.base); // 使用连接池，避免开太多的线程，提升性能
const setLoginLog = (params) => {
    const pool = mysqlDB
    const sqlString = sql['user'].setLoginLog
    pool.getConnection((err, connection) => {
        if (!err) {
            connection.query(sqlString, params, (err, result) => {
                if (err) {
                    console.log(err);
                }
                connection.release();
            });
        } else {
            console.log(err);
        }
    });
}
/**
 * 
 * @param {* 请求体} req 
 * @param {* 请求类型 add,update,delete} operation 
 * @param {* 结果} results 
 */
const setOperationLog = (req,  operation,results) => {
  let params = JSON.stringify(req.body);
  let title = decodeURIComponent(req.headers["title"]);
  let ip = req.ip;
  ip = ip.replace(/^.*:(.*)$/, "$1");
//   console.log("请求方法:", req.method); // 获取请求方法
//   console.log("请求 URL:", req.url); // 获取请求 URL
//   console.log("请求 ip:", ip); // 获取请求 ip
//   console.log("请求 user:", req.user.account); // 获取请求 用户
//   console.log("请求 pagePath:", req.headers["pagepath"]); // 获取请求 页面路径
  const userAgent = req.headers["user-agent"];
  let operator_type = "0";
  if (/mobile/i.test(userAgent)) {
    operator_type = "2";
  } else {
    operator_type = "1";
  }
    let address = "";
  let status = '0'
  let business_type = "0";
  let results2 = ''
  switch (operation) {
    case "add":
      business_type = "1";
      results2 = '{ "msg": "添加成功", "code": 200 }';
      status = "0";
      break;
    case "update":
      business_type = "2";
      results2 = '{ "msg": "更改成功", "code": 200 }';
      status = "0";
      break;
    case "delete":
      business_type = "3";
      results2 = '{ "msg": "删除成功", "code": 200 }';
      status = "0";
      break;
    case "select":
      results2 = '{ "msg": "查找成功", "code": 200 }';
      status = "0";
      break;
    case "selectall":
      results2 = '{ "msg": "全部查找成功", "code": 200 }';
      status = "0";
      break;
    case "undefined":
      results2 = '{ "msg": "操作失败", "code": 300 }';
      status = "1";
      break;
    case undefined:
      results2 = '{ "msg": "操作失败", "code": 300 }';
      status = "1";
      break;
    default:
      status = "0";
      business_type = "0";
      break;
  }
  if (results) {
    results = JSON.stringify(results) || results;
  } else {
    results = results2
  }
  let paraArr = [
    title,
    business_type,
    req.method,
    operator_type,
    req.user.account,
    req.url,
    ip,
    params,
    results,
    status,
  ];
  //   try {
  //     axios
  //       .get(`http://ip-api.com/json/${ip}`)
  //       .then((response) => {
  //         const location = response.data;
  //         console.log("用户 IP:", ip);
  //         console.log("用户位置:", location);
  //         address =
  //           location.country + " " + location.regionName + " " + location.city;
  //         next();
  //       })
  //       .catch((error) => {
  //         console.error("获取地理位置时出错:", error);
  //         next();
  //       });
  //   } catch (error) {
  //     console.log(174, error);
  //     }
  const pool = mysqlDB;
  const sqlString = sql["user"].setOperationLog;
  pool.getConnection((err, connection) => {
    if (!err) {
      connection.query(sqlString, paraArr, (err, result) => {
        if (err) {
          console.log(err);
        }
        connection.release();
      });
    } else {
      console.log(err);
    }
  });
};

module.exports = { setLoginLog, setOperationLog };